
//
//  CourserSwitchingView.m
//  ShowYork
//
//  Created by hxh on 2020/9/3.
//  Copyright © 2020 hxh. All rights reserved.
//

#import "LessonSwitchingView.h"

@interface LessonSwitchingView ()
@property (nonatomic,strong)UIView *containView;
@property (nonatomic,strong)UIView *shadowView;
@property (nonatomic,strong)NSArray *titleArray;
@property (nonatomic,assign)CGPoint point;
@property (nonatomic,assign)NSInteger index;
@property (nonatomic,strong)UIView *line;

@property (nonatomic,strong)NSMutableArray *checkImgArray;
@property (nonatomic,strong)NSMutableArray *checkLbArray;


@end

@implementation LessonSwitchingView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (NSMutableArray *)checkImgArray{
    if (!_checkImgArray) {
        _checkImgArray = [NSMutableArray array];
    }
    return _checkImgArray;
}

- (NSMutableArray *)checkLbArray{
    if (!_checkLbArray) {
        _checkLbArray = [NSMutableArray array];
    }
    return _checkLbArray;
}
- (instancetype)initWithTitleArray:(NSArray *)titleArray touchPoint:(CGPoint)point index:(NSInteger)index{
    if (self = [super init]) {
        self.titleArray = titleArray;
        self.point = point;
        self.index = index;
        [self setUI];
    }
    return self;
}

- (void)setUI{
    self.frame = CGRectMake(0, 0, kScreenW, kScreenH);
    
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)];
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)];
    
    [self addGestureRecognizer:pan];
    [self addGestureRecognizer:tap];
    
    self.shadowView = [[UIView alloc]init];
    [self addSubview:self.shadowView];
       
    [self.shadowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(self.point.y);
        make.height.mas_equalTo(72);
        make.left.right.equalTo(self);
    }];
    
    self.containView = [[UIView alloc]init];
    self.containView.backgroundColor = [UIColor whiteColor];
    [self.shadowView addSubview:self.containView];
    
    [self.containView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(self.shadowView);
    }];

    [self.titleArray enumerateObjectsUsingBlock:^(NSString * _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        UILabel *lb = [[UILabel alloc]init];
        lb.text = obj;
        lb.font = font(14);
        lb.textColor = idx == self.index?[RPAppManager themColor]:RGBA(51, 51, 51, 1);
        [self addSubview:lb];
        
        [lb mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.containView).offset(idx * 36);
            make.left.equalTo(self).offset(self.point.x);
            make.height.mas_equalTo(36);
        }];
        
        UIImageView *checkImgView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"course_tag_tick"]];
        checkImgView.hidden = self.index != idx;
        [self addSubview:checkImgView];
        
        [checkImgView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(lb);
            make.right.equalTo(lb.mas_left).offset(-5);
        }];
    
        UIButton *btn = [[UIButton alloc]init];
        btn.tag = idx;
        [btn addTarget:self action:@selector(coverMethod:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:btn];
        
        [btn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(lb);
            make.left.right.equalTo(self.containView);
            make.height.mas_offset(36);
        }];
        
        [self.checkLbArray addObject:lb];
        [self.checkImgArray addObject:checkImgView];
    
    }];
    
    [self.containView layoutIfNeeded];

    
    self.shadowView.layer.shadowColor   = [UIColor lightGrayColor].CGColor;
    self.shadowView.layer.shadowOffset  = CGSizeMake(0, 0);
    self.shadowView.layer.shadowOpacity = 0.8;
    self.shadowView.layer.shadowRadius  = 1;

    UIBezierPath *maskPath = [UIBezierPath bezierPathWithRoundedRect:self.containView.bounds byRoundingCorners:UIRectCornerBottomLeft | UIRectCornerBottomRight cornerRadii:CGSizeMake(15, 15)];
    CAShapeLayer *maskLayer = [[CAShapeLayer alloc] init];
    maskLayer.frame = self.containView.bounds;
    maskLayer.path = maskPath.CGPath;
    self.containView.layer.mask = maskLayer;

    // 根据index来切换
    
}

- (void)show{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
}

- (void)coverMethod:(UIButton*)btn{
    self.index = btn.tag;
    [self dismiss];
}

- (void)dismiss{
    if (self.done) {
        self.done(self.index);
    }
    [self removeFromSuperview];
}

@end
